/**
 * Copyright (C) Octlan Corporation. All rights reserved.
 * 
 * @author todzhang
 * @date 2010-2-24
 * @version $Id: SMSReceiver.java 9 2010-02-26 07:48:39Z todzhang $
 */
package com.octlan.droid.mii.receiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver {

	private static final String	LOG_TAG	= "SMSApp";
	/* package */
	static final String			ACTION	= "android.provider.Telephony.SMS_RECEIVED";

	@Override
	public void onReceive(Context context, Intent intent) {

		if (intent.getAction().equals(ACTION)) {
			StringBuilder buf = new StringBuilder();
			Bundle bundle = intent.getExtras();
			if (bundle != null) {
				Object messages[] = (Object[]) bundle.get("pdus");
				SmsMessage smsMessage[] = new SmsMessage[messages.length];
				for (int i = 0; i < messages.length; i++) {
					smsMessage[i] = SmsMessage.createFromPdu((byte[]) messages[i]);
					SmsMessage message = smsMessage[i];
					buf.append("Received SMS from  ");
					buf.append(message.getDisplayOriginatingAddress());
					buf.append(" - ");
					buf.append(message.getDisplayMessageBody());
				}
			}
			Log.i(LOG_TAG, "[SMSApp] onReceiveIntent: " + buf);
			// ---display the new SMS message---
			Toast.makeText(context, buf, Toast.LENGTH_SHORT).show();
			/*
			 * NotificationManager nm = (NotificationManager) context
			 * .getSystemService(Context.NOTIFICATION_SERVICE);
			 * 
			 * Notification notification = new Notification(123, buf, System
			 * .currentTimeMillis());
			 * 
			 * nm.notify(123, notification);
			 */

		}
	}

}
